﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using SASS.Sociales.BE;
using SIASS.Soluciones.Data;
using SASS.Sociales.Util;

namespace SASS.Sociales.DAL 
{
    public class ActorDAL: SIASSClaseBase
    {

        #region "Declaracion de Variables"

        #endregion

        #region "Consultas"

        public ActorBE Obtener_Actor_SELPK(string codigoActor)
        {
            SqlCommand cmd = new SqlCommand("usp_siass_actor_selpk");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@P_ACT_CODIGO", SqlDbType.Int).Value = Convert.ToInt32(codigoActor);

            using (SqlDataReader dr = BaseDatos.GetDataReader(cmd))
            {
                ActorBE oActor = new ActorBE();
                if (dr.HasRows)
                {
                    if (dr.Read())
                    {
                        oActor.Codigo = Convert.ToInt32(this.getNumericValue(dr, 0));
                        oActor.Nombre = this.getStringValue(dr, 1);
                        oActor.RazonSocial = this.getStringValue(dr, 2);
                        oActor.Rubro = this.getStringValue(dr, 3);
                        oActor.Descripcion = this.getStringValue(dr, 4);
                        oActor.Ruc  = this.getStringValue(dr, 5);
                        oActor.Ubigeo = this.getStringValue(dr, 6);
                        oActor.Direccion = this.getStringValue(dr, 7);
                        oActor.Telefono = this.getStringValue(dr, 8);
                        oActor.Email = this.getStringValue(dr, 9);
                        oActor.Website = this.getStringValue(dr, 10);                       
                    }
                }
                return oActor;
            }
            
        }
        
        public List<ActorBE> Listar_Actor_SELALL(string condicion)
        {

            List<ActorBE> lstActor = new List<ActorBE>();
            SqlCommand cmd = new SqlCommand("usp_siass_actor_selall");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@P_CONDICION", SqlDbType.VarChar).Value = condicion.Trim();

            using (SqlDataReader dr = BaseDatos.GetDataReader(cmd))
            {
                
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        ActorBE oActor = new ActorBE();
                        oActor.Codigo = Convert.ToInt32(this.getNumericValue(dr, 0));
                        oActor.Nombre = this.getStringValue(dr, 1);
                        oActor.RazonSocial = this.getStringValue(dr, 2);
                        oActor.Rubro = this.getStringValue(dr, 3);
                        oActor.Descripcion = this.getStringValue(dr, 4);
                        oActor.Ruc = this.getStringValue(dr, 5);
                        oActor.Ubigeo = this.getStringValue(dr, 6);
                        oActor.Direccion = this.getStringValue(dr, 7);
                        oActor.Telefono = this.getStringValue(dr, 8);
                        oActor.Email = this.getStringValue(dr, 9);
                        oActor.Website = this.getStringValue(dr, 10);
                        lstActor.Add(oActor);
                    }
                }                
            }
            return lstActor;

        }

        #endregion

        #region "Mantenimiento"

        public bool Insertar_SIASS_Actor(ActorBE oActor)
        {

            bool exito = false;
            int rowInsert;

            using (SqlConnection conn = BaseDatos.GetConnection())
            {

                SqlTransaction oTransaction = null;
                oTransaction = conn.BeginTransaction();
                try
                {
                    SqlCommand cmd = new SqlCommand("[dbo].[usp_siass_actor_insert]");
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@P_ACT_CODIGO", SqlDbType.Int).Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@P_ACT_NOMBRE", SqlDbType.VarChar).Value = oActor.Nombre;
                    cmd.Parameters.Add("@P_ACT_RZN_SOCIAL", SqlDbType.VarChar).Value = oActor.RazonSocial;
                    cmd.Parameters.Add("@P_ACT_RUBRO", SqlDbType.VarChar).Value = oActor.Rubro;
                    cmd.Parameters.Add("@P_ACT_DESCRIPCION", SqlDbType.VarChar).Value = oActor.Descripcion;
                    cmd.Parameters.Add("@P_ACT_RUC", SqlDbType.VarChar).Value = oActor.Ruc;
                    cmd.Parameters.Add("@P_ACT_UBIGEO", SqlDbType.VarChar).Value = oActor.Ubigeo;
                    cmd.Parameters.Add("@P_ACT_DIRECCION", SqlDbType.VarChar).Value = oActor.Direccion;
                    cmd.Parameters.Add("@P_ACT_TELEFONO", SqlDbType.VarChar).Value = oActor.Telefono;
                    cmd.Parameters.Add("@P_ACT_EMAIL", SqlDbType.VarChar).Value = oActor.Email;
                    cmd.Parameters.Add("@P_ACT_URL", SqlDbType.VarChar).Value = oActor.Website;
                    cmd.Parameters.Add("@P_ACT_TIP_ENTIDAD", SqlDbType.VarChar).Value = oActor.TipoEntidad;
                    cmd.Parameters.Add("@P_CREADOR_POR", SqlDbType.VarChar).Value = SIASSGenerica.USR_CodigoUsuario;
                    cmd.Parameters.Add("@P_IP_CREACION", SqlDbType.VarChar).Value = SIASSGenerica.ComputerID;
                    cmd.Parameters.Add("@P_MODIFICADO_POR", SqlDbType.VarChar).Value = SIASSGenerica.USR_CodigoUsuario; ;
                    cmd.Parameters.Add("@P_IP_MODIFICACION", SqlDbType.VarChar).Value = SIASSGenerica.ComputerID;                    
                    cmd.Parameters.Add("@P_FLAGESTADO", SqlDbType.VarChar).Value = "ACTIV";
                    
                    rowInsert = BaseDatos.Execute(cmd, oTransaction);

                    if (rowInsert > 0)
                    {
                        oTransaction.Commit();
                        oActor.Codigo = Convert.ToInt32(cmd.Parameters["@P_ACT_CODIGO"].Value);
                        exito = true;
                    }
                    else {
                        oTransaction.Rollback();
                    }

                }
                catch (Exception ex){
                    throw ex;
                }
                finally {
                    oTransaction.Dispose();
                    if (conn.State.Equals(ConnectionState.Open)){
                        conn.Close();
                    }
                }
            }
            return exito;
        }

        public bool Actualizar_SIASS_Actor(ActorBE oActor)
        {

            bool exito = false;
            int rowUpdate;

            using (SqlConnection conn = BaseDatos.GetConnection())
            {

                SqlTransaction oTransaction = null;
                oTransaction = conn.BeginTransaction();
                try
                {
                    SqlCommand cmd = new SqlCommand("[dbo].[usp_siass_actor_update]");
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@P_ACT_CODIGO", SqlDbType.Int).Value = oActor.Codigo;
                    cmd.Parameters.Add("@P_ACT_NOMBRE", SqlDbType.VarChar).Value = oActor.Nombre;
                    cmd.Parameters.Add("@P_ACT_RZN_SOCIAL", SqlDbType.VarChar).Value = oActor.RazonSocial;
                    cmd.Parameters.Add("@P_ACT_RUBRO", SqlDbType.VarChar).Value = oActor.Rubro;
                    cmd.Parameters.Add("@P_ACT_DESCRIPCION", SqlDbType.VarChar).Value = oActor.Descripcion;
                    cmd.Parameters.Add("@P_ACT_RUC", SqlDbType.VarChar).Value = oActor.Ruc;
                    cmd.Parameters.Add("@P_ACT_UBIGEO", SqlDbType.VarChar).Value = oActor.Ubigeo;
                    cmd.Parameters.Add("@P_ACT_DIRECCION", SqlDbType.VarChar).Value = oActor.Direccion;
                    cmd.Parameters.Add("@P_ACT_TELEFONO", SqlDbType.VarChar).Value = oActor.Telefono;
                    cmd.Parameters.Add("@P_ACT_EMAIL", SqlDbType.VarChar).Value = oActor.Email;
                    cmd.Parameters.Add("@P_ACT_URL", SqlDbType.VarChar).Value = oActor.Website;
                    cmd.Parameters.Add("@P_ACT_TIP_ENTIDAD", SqlDbType.VarChar).Value = oActor.TipoEntidad;
                    cmd.Parameters.Add("@P_CREADOR_POR", SqlDbType.VarChar).Value = SIASSGenerica.USR_CodigoUsuario;
                    cmd.Parameters.Add("@P_IP_CREACION", SqlDbType.VarChar).Value = SIASSGenerica.ComputerID;
                    cmd.Parameters.Add("@P_MODIFICADO_POR", SqlDbType.VarChar).Value = SIASSGenerica.USR_CodigoUsuario; ;
                    cmd.Parameters.Add("@P_IP_MODIFICACION", SqlDbType.VarChar).Value = SIASSGenerica.ComputerID;                    
                    cmd.Parameters.Add("@P_FLAGESTADO", SqlDbType.VarChar).Value = "ACTIV";
                    
                    rowUpdate = BaseDatos.Execute(cmd, oTransaction);

                    if (rowUpdate > 0)
                    {
                        oTransaction.Commit();
                        exito = true;
                    }
                    else {
                        oTransaction.Rollback();
                    }

                }
                catch (Exception ex){
                    throw ex;
                }
                finally {
                    oTransaction.Dispose();
                    if (conn.State.Equals(ConnectionState.Open)){
                        conn.Close();
                    }
                }
            }
            return exito;
        }
        
        
        #endregion

    }
}
